# Maintainer: Daurnimator <daurnimator@archlinux.org>

pkgname=argocd
pkgver=2.7.1
# find matching argo-ui commit via ui/yarn.lock
# commit id also needs to be updated in argo-ui.patch
_argoui_commit=6107e270e7090cf9af37bf43d017dbd17d3d9130
pkgrel=1
pkgdesc="Declarative continuous deployment for Kubernetes"
arch=('loong64' 'x86_64')
url='https://argo-cd.readthedocs.io/en/stable/'
license=('Apache')
makedepends=('go' 'python' 'yarn')
depends=('glibc')
provides=('argocd-cli')
replaces=('argocd-cli')
source=("argo-cd-${pkgver}.tar.gz::https://github.com/argoproj/argo-cd/archive/v${pkgver}.tar.gz"
        "argo-ui-${_argoui_commit}.tar.gz::https://github.com/argoproj/argo-ui/archive/${_argoui_commit}.tar.gz"
        "argo-ui.patch")
sha512sums=('472ecb1d77290e9f63e18ec6d48a4f0129ecc1039ffe7bc9778d6d1f74017b37c01055ac285952050d278f78c03fb44e2bd62dbd573c1f2285cec2aa73dec8aa'
            'b35ae2803391586774cd9a8eacefae061ae42d28b560d6e64566cec6615705007ee671a5ff624fd333c6a439d713e1dba5822c5991db2cb0b0c2054950a92b93'
            'c2875512b49cdfee3101ea9f9a6e9edda073c4a46187450a31d38ab7dc6b9a57fc15912b3e1cefebe740c5680ca9ee5f4db11760186a3526957ad8a417e9abaa')

prepare() {
  cd "argo-cd-$pkgver"

  patch -p0 -i ../argo-ui.patch

  export GOPATH="${srcdir}"
  go mod download
}

build() {
  cd "argo-cd-$pkgver"

  (
    cd ui
    yarn install --cache-folder "${srcdir}/yarn-cache"
    ARGO_VERSION="$pkgver" NODE_ENV='production' NODE_ONLINE_ENV='online' yarn build
  )

  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GOPATH="${srcdir}"
  export GOPROXY=off
  go build -o argocd \
    -trimpath \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-X github.com/argoproj/argo-cd/v2/common.version=${pkgver} \
              -X github.com/argoproj/argo-cd/v2/common.buildDate=$(date -u +'%Y-%m-%dT%H:%M:%SZ' --date=@${SOURCE_DATE_EPOCH}) \
              -X github.com/argoproj/argo-cd/v2/common.gitTag=${pkgver} \
              -compressdwarf=false \
              -linkmode=external \
              -extldflags \"${LDFLAGS}\"" \
    ./cmd

  ./argocd completion bash > argocd.bash
  ./argocd completion zsh > argocd.zsh
}

package() {
  cd "argo-cd-$pkgver"

  install -D argocd "$pkgdir/usr/bin/argocd"
  ln -s argocd "$pkgdir/usr/bin/argocd-server"
  ln -s argocd "$pkgdir/usr/bin/argocd-application-controller"
  ln -s argocd "$pkgdir/usr/bin/argocd-repo-server"
  ln -s argocd "$pkgdir/usr/bin/argocd-cmp-server"
  ln -s argocd "$pkgdir/usr/bin/argocd-dex"
  ln -s argocd "$pkgdir/usr/bin/argocd-notifications"
  ln -s argocd "$pkgdir/usr/bin/argocd-git-ask-pass"

  install -Dm644 argocd.bash "$pkgdir/usr/share/bash-completion/completions/argocd"
  install -Dm644 argocd.zsh "$pkgdir/usr/share/zsh/site-functions/_argocd"
}
